//
//  JEHVideoTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/8.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHVideoTableViewCell.h"

@interface JEHVideoTableViewCell ()

@property (nonatomic, retain) UILabel *replyLabel;
@property (nonatomic, assign) BOOL isPlay;

@end

@implementation JEHVideoTableViewCell

- (void)dealloc
{
    [_rootModel release];
    [_player release];
    [_titleLabel release];
    [_replyLabel release];
    [_picture release];
    [_playPicture release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_titleLabel release];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    self.replyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_replyLabel release];
    _replyLabel.font = [UIFont systemFontOfSize:10];
    _replyLabel.textColor = [UIColor grayColor];
    self.picture = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_picture release];
    _picture.userInteractionEnabled = YES;
    self.playPicture = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_playPicture release];
    _playPicture.image = [UIImage imageNamed:@"play"];

    [self addSubview:_picture];
    [self addSubview:_titleLabel];
    [self addSubview:_replyLabel];
    [self addSubview:_playPicture];
}

- (void)setRootModel:(JEHRootModel *)rootModel {
    if (_rootModel != rootModel) {
        [_rootModel release];
        _rootModel = [rootModel retain];
    }
    self.titleLabel.text = rootModel.title;
    self.replyLabel.text = [NSString stringWithFormat:@"%ld播放", rootModel.videoModel.playnumber];
    if (rootModel.videoModel.playnumber > 10000) {
        
        self.replyLabel.text = [NSString stringWithFormat:@"%ld万播放", rootModel.videoModel.playnumber / 10000];
    }
    [self.picture sd_setImageWithURL:[NSURL URLWithString:rootModel.kpic] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.isPlay = NO;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat replyWidth = [self getWidthWithText:self.replyLabel.text withFont:10];
    self.titleLabel.frame = CGRectMake(10, self.height - 30, self.width - 10 - replyWidth, 30);
    self.replyLabel.frame = CGRectMake(self.width - replyWidth - 10, self.height - 30, replyWidth, 30);
    self.picture.frame = CGRectMake(5, 5, self.width - 10, self.height - 35);
    self.playPicture.frame = CGRectMake(self.width / 2 - 50, self.height / 2 - 50, 100, 100);
}

- (CGFloat)getWidthWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
